package org.andy.shop.controller;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.andy.shop.dao.GoodMapper;
import org.andy.shop.model.Good;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value="/good",produces="application/json;charset=UTF-8")
public class GoodController {

	@Resource
	private GoodMapper goodMapper;
	
	@PostMapping("/list")
	@ResponseBody
	private String validUserName(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		Good good = new Good();
		List<Good> goods = goodMapper.list(good);
		json.put("result", true);
		json.put("rows", goods.toArray());
		return json.toJSONString();
	}
	
	@PostMapping("/file")
	private JSONObject upload(HttpServletRequest request,@RequestParam MultipartFile file) {
		JSONObject json = new JSONObject();
		try {
			file.transferTo(new File("/Users/luxiaobai/临时/img/good/"+file.getOriginalFilename()));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		json.put("status", new File("/Users/luxiaobai/临时/img/good/"+file.getOriginalFilename()).exists());
		return json;
	}
	
	@GetMapping("/file")
	private ResponseEntity<byte[]> download(@RequestParam String fileName){
		String filePath = "/Users/luxiaobai/临时/img/good/" + fileName;
		File file = new File(filePath);
		String downloadFileName = null;
		try {
			downloadFileName = URLEncoder.encode(fileName,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", downloadFileName);
		byte[] fileArray = null;
		try {
			fileArray = FileUtils.readFileToByteArray(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(fileArray,headers,HttpStatus.CREATED);
		return responseEntity;
	}
	
	@GetMapping("/file2")
	private void download2(@RequestParam String fileName,HttpServletResponse response){
		String filePath = "/Users/luxiaobai/临时/img/good/" + fileName;
		File file = new File(filePath);
		String downloadFileName = null;
		try {
			downloadFileName = URLEncoder.encode(fileName,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] fileArray = null;
		try {
			fileArray = FileUtils.readFileToByteArray(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		response.setContentType("application/x-download");
		try {
			response.setHeader("Content-Disposition", "attachment;filename="+new String(downloadFileName.getBytes("utf-8"),"ISO8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.setContentLength(fileArray.length);
		try {
			response.getOutputStream().write(fileArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@DeleteMapping("/file")
	private boolean delete(String filePath) {
		return FileUtils.deleteQuietly(new File(filePath));
	}
}
